// index.js
// 获取应用实例
const app = getApp()
import {
    util,
    Toast,
    Dialog,
    commonMixin
} from "../../utils/util.js";

function scrollNum(minNum, maxNum, top, start, end) {
    return start + ((top - minNum) / (maxNum - minNum) * (end - start));
}
Page({
    behaviors: [commonMixin],
    data: {
        showPopup: false,
        vertical: false,
        autoplay: false,
        interval: 2000,
        duration: 500,
        swiperIndex: 0,
        banners: [], //头部banner
        selectedCheckItems: [],
    },
    onLoad() {
      wx.showLoading({
        title: '加载中',
      })
      util.http.post("v1/mini/home-banners",{}).then((res) => {
          if (res.data.code == 0) {
            this.setData({
              banners: res.data.data
            })
          } else {
              Toast(res.data.message);
          }
          wx.hideLoading();
      }).catch((err) => {
          wx.hideLoading();
          Toast('网络异常！刷新后重试！');
          console.log(err)
      })
      util.http.post("v1/mini/home-selected-goods",{}).then((res) => {
          if (res.data.code == 0) {
            this.setData({
              selectedCheckItems: res.data.data
            })
          } else {
              Toast(res.data.message);
          }
          wx.hideLoading();
      }).catch((err) => {
          wx.hideLoading();
          Toast('网络异常！刷新后重试！');
          console.log(err)
      })
    },
    onShow() {
      
    },
    swiperChange(e) {
        this.setData({
            swiperIndex: e.detail.current
        })
    },
    onShareAppMessage() {
   
    },
    handleJumpCheck() {
        wx.navigateTo({
          url: '/pages/check/index',
        })
    },
    handleJumpReport() {
        wx.switchTab({
          url: '/pages/report/index',
        })
    },
    handleScan() {
        wx.scanCode({  
            success: function (res) {  
                if (res.result.indexOf('https://wechat.lychix.com/s/') != -1) {
                    let sampleSn = res.result.replace('https://wechat.lychix.com/s/', '');
                    wx.showLoading({
                        title: '加载中',
                    })
                    util.http.post("v1/mini/sample-bind-user",{
                        sample_sn: sampleSn
                    }).then((res) => {
                        if (res.data.code == 0) {
                            Toast('绑定成功！');
                            setTimeout(() => {
                                wx.navigateTo({
                                    url: '/pages/check/index?id=' + res.data.data.id,
                                })
                            }, 2000)
                        } else {
                            Toast(res.data.message);
                        }
                        wx.hideLoading();
                    }).catch((err) => {
                        wx.hideLoading();
                        Toast('网络异常！刷新后重试！');
                        console.log(err)
                    })
                }
            },  
            fail: function (err) {  
                Toast('扫码出错！');
            }  
        });
    },
})